package edu.gatech.coc.betameisters.userinterfaces;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JRadioButton;

import edu.gatech.coc.betameisters.infoholders.MedicalHistory;
import edu.gatech.coc.betameisters.infoholders.Treatment;
import edu.gatech.coc.betameisters.main.MainPanel;
import edu.gatech.coc.betameisters.people.Patient;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class EditMedicalHist extends JPanel {
	private JTable treatmentListTable;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtState;
	private JTextField txtZipCode;
	private JTextField txtInsurance;
	private JTextField txtAreaCode;
	private JTextField txtPhoneNext;
	private JTextField txtPhoneLast;
	private JTextField textFieldMonth;
	private JTextField textFieldDay;
	private JTextField textFieldYear;
	private JTextField textAge;

	/**
	 * 
	 */
	private static final long serialVersionUID = 6735289914189521652L;
	@SuppressWarnings("unused")
	private MainPanel myParent;
	private JTextField txtLastName;
	private JTextField txtFirstName;
	
	/**
	 * Create the panel.
	 */
	public EditMedicalHist(final Patient patient, MedicalHistory thisMedHist, MainPanel p) {
		myParent = p;
		setLayout(null);
		
		JButton btnSubmit = new JButton("Submit Changes");
		btnSubmit.setBounds(294, 534, 126, 23);
		add(btnSubmit);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				myParent.setPatient(patient);
			}
		});
		btnCancel.setBounds(430, 534, 106, 23);
		add(btnCancel);
		
		JLabel lblYourMedicalInformation = new JLabel("Your Medical Information");
		lblYourMedicalInformation.setBounds(225, 11, 155, 14);
		add(lblYourMedicalInformation);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(10, 37, 91, 14);
		add(lblLastName);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(129, 37, 87, 14);
		add(lblFirstName);
		
		JLabel lblDateOfBirth = new JLabel("Date of Birth");
		lblDateOfBirth.setBounds(10, 87, 108, 14);
		add(lblDateOfBirth);
		
		JLabel lblStreetAddress = new JLabel("Street Address");
		lblStreetAddress.setBounds(10, 219, 131, 14);
		add(lblStreetAddress);
		
		JLabel lblInsuranceProvider = new JLabel("Insurance Provider");
		lblInsuranceProvider.setBounds(10, 303, 165, 14);
		add(lblInsuranceProvider);
		
		treatmentListTable = new JTable();
		treatmentListTable.setBounds(10, 374, 526, 87);
		add(treatmentListTable);
		treatmentListTable.setModel(new DefaultTableModel(new Object[50][2],
				new String[] { "Treatment Type", "Description"}));
		List<Treatment> record = thisMedHist.getTreatmentRecord();
		Treatment temp;
		int i = 0;
		while (!record.isEmpty()){
			// search for a certain day and print all the information
			temp = record.remove(0);
			treatmentListTable.setValueAt(temp.getTreatmentType().toString(), i, 0);
			treatmentListTable.setValueAt(temp.getTreatmentDetails(),i,1);
			i++;
		}
		
		JLabel lblTreatmentHistory = new JLabel("Treatment History");
		lblTreatmentHistory.setBounds(10, 353, 131, 14);
		add(lblTreatmentHistory);
		
		JLabel lblGender = new JLabel("Gender");
		lblGender.setBounds(198, 109, 46, 14);
		add(lblGender);
		
		JLabel lblOtherMedicalInformation = new JLabel("Other Medical Information");
		lblOtherMedicalInformation.setBounds(10, 472, 177, 14);
		add(lblOtherMedicalInformation);
		
		String thisGender;
		if(thisMedHist.getGender() == null){
			thisGender = "";
		}
		else{
			thisGender = thisMedHist.getGender().toString();
		}
		JLabel label = new JLabel(thisGender);
		label.setBounds(198, 128, 58, 14);
		add(label);
		
		JLabel lblAge = new JLabel("Age");
		lblAge.setBounds(154, 106, 46, 14);
		add(lblAge);
		
		txtAddress = new JTextField();
		txtAddress.setText(thisMedHist.getAddress());
		txtAddress.setBounds(10, 232, 303, 20);
		add(txtAddress);
		txtAddress.setColumns(10);
		
		JLabel lblCity = new JLabel("City");
		lblCity.setBounds(14, 263, 87, 14);
		add(lblCity);
		
		txtCity = new JTextField();
		txtCity.setText(thisMedHist.getCity());
		txtCity.setBounds(10, 278, 108, 20);
		add(txtCity);
		txtCity.setColumns(10);
		
		JLabel lblState = new JLabel("State");
		lblState.setBounds(129, 263, 86, 14);
		add(lblState);
		
		txtState = new JTextField();
		txtState.setText(thisMedHist.getState());
		txtState.setBounds(129, 278, 86, 20);
		add(txtState);
		txtState.setColumns(10);
		
		JLabel lblZipCode = new JLabel("Zip Code");
		lblZipCode.setBounds(225, 263, 106, 14);
		add(lblZipCode);
		
		txtZipCode = new JTextField();
		txtZipCode.setText(thisMedHist.getZipCode() + "");
		txtZipCode.setBounds(225, 278, 86, 20);
		add(txtZipCode);
		txtZipCode.setColumns(10);
		
		txtInsurance = new JTextField();
		txtInsurance.setText(thisMedHist.getInsurance());
		txtInsurance.setBounds(10, 322, 165, 20);
		add(txtInsurance);
		txtInsurance.setColumns(10);
		
		JLabel lblContactNumber = new JLabel("Contact Number");
		lblContactNumber.setBounds(10, 163, 131, 14);
		add(lblContactNumber);
		
		char[] phnum = {0,0,0,0,0,0,0,0,0,0};
		if(thisMedHist.getPhoneNumber() != null){
			phnum = thisMedHist.getPhoneNumber().toCharArray();
		}
		
		txtAreaCode = new JTextField();
		txtAreaCode.setBounds(10, 177, 34, 20);
		txtAreaCode.setText(phnum[0] + phnum[1] + phnum[2] + "");
		add(txtAreaCode);
		txtAreaCode.setColumns(10);
		
		txtPhoneNext = new JTextField();
		txtPhoneNext.setBounds(54, 177, 34, 20);
		txtPhoneNext.setText(phnum[3] + phnum[4] + phnum[5] + "");
		add(txtPhoneNext);
		txtPhoneNext.setColumns(10);
		
		txtPhoneLast = new JTextField();
		txtPhoneLast.setText(phnum[6] + phnum[7] + phnum[8] + phnum[9] + "");
		txtPhoneLast.setBounds(98, 177, 43, 20);
		add(txtPhoneLast);
		txtPhoneLast.setColumns(10);
		
		JTextArea txtrAdfs = new JTextArea();
		txtrAdfs.setText(thisMedHist.getOtherInfo());
		txtrAdfs.setBounds(10, 497, 274, 60);
		add(txtrAdfs);
		
		JLabel lblMonth = new JLabel("Month");
		lblMonth.setBounds(10, 106, 46, 14);
		add(lblMonth);
		
		JLabel lblDay = new JLabel("Day");
		lblDay.setBounds(55, 106, 46, 14);
		add(lblDay);
		
		JLabel lblYear = new JLabel("Year");
		lblYear.setBounds(98, 106, 46, 14);
		add(lblYear);
		
		
		char[] dob = {0,1,0,1,1,9,9,1};
		if(thisMedHist.getDateOfBirth() != null){
			dob = thisMedHist.getDateOfBirth().toCharArray();
		}
		int month = Integer.parseInt(dob[0] + dob[1] + "");
		int day = Integer.parseInt(dob[2] + dob[3] + "");
		int year = Integer.parseInt(dob[4] + dob[5] + dob[6] + dob[7] + "");
		textFieldMonth = new JTextField();
		textFieldMonth.setBounds(10, 122, 34, 20);
		textFieldMonth.setText(month + "");
		add(textFieldMonth);
		textFieldMonth.setColumns(10);
		
		textFieldDay = new JTextField();
		textFieldDay.setBounds(55, 122, 34, 20);
		textFieldDay.setText(day + "");
		add(textFieldDay);
		textFieldDay.setColumns(10);
		
		textFieldYear = new JTextField();
		textFieldYear.setBounds(98, 122, 43, 20);
		textFieldYear.setText(year + "");
		add(textFieldYear);
		textFieldYear.setColumns(10);
		
		textAge = new JTextField();
		textAge.setText(calcAge(month, day, year) + "");
		textAge.setEditable(false);
		textAge.setBounds(154, 122, 34, 20);
		add(textAge);
		textAge.setColumns(10);
		
		txtLastName = new JTextField();
		txtLastName.setBounds(15, 56, 86, 20);
		add(txtLastName);
		txtLastName.setColumns(10);
		
		txtFirstName = new JTextField();
		txtFirstName.setBounds(129, 56, 86, 20);
		add(txtFirstName);
		txtFirstName.setColumns(10);
		/*
		JPanel panel = new Graph(patient.getMedHist().getWeightHis(),patient.getMedHist().getHeightHis());
		panel.setBounds(310, 36, 274, 306);
		add(panel)
		*/;
	}

	private int calcAge(int month, int day, int year) {
		int currentAge;
		Calendar today = new GregorianCalendar();
		currentAge = today.get(Calendar.YEAR) - year;
		if(today.get(Calendar.MONTH) > month){
			currentAge++;
		}
		else if(today.get(Calendar.MONTH) == month){
			if(today.get(Calendar.DAY_OF_MONTH) >= day){
				currentAge++;
			}
		}
		return currentAge;
	}
}
